/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFilterListPanel.java
 *
 * Created on 2010-5-23, 10:19:36
 */

package cn.ekuma.epos.analysis.situation;

import com.openbravo.data.gui.ComboBoxValEntry;
import com.openbravo.pos.base.AppLocal;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractListModel;

/**
 *
 * @author Administrator
 */
public abstract class JFilterListPanel extends javax.swing.JPanel {
     List<ComboBoxValEntry> selectedObjs;
     ValEntryListModel listModel;
    /** Creates new form JFilterListPanel */
    public JFilterListPanel() {
        initComponents();
        selectedObjs=new ArrayList();
        listModel=new ValEntryListModel();
        jList1.setModel(listModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.BorderLayout());

        jButton1.setText(AppLocal.getIntString("button.New")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel9.add(jButton1);

        jButton2.setText(AppLocal.getIntString("buttom.delete")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel9.add(jButton2);

        jButton3.setText(AppLocal.getIntString("buttom.removeAll")); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel9.add(jButton3);

        jPanel1.add(jPanel9, java.awt.BorderLayout.PAGE_START);

        jScrollPane1.setViewportView(jList1);

        jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ComboBoxValEntry addObj=add();
        if(addObj!=null){
            listModel.add(addObj);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       int selected=jList1.getSelectedIndex();
       if(selected>=0){
           listModel.remove(selected);
       }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        listModel.rest();
    }//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables


    public abstract ComboBoxValEntry add();

    public void removeAll(){
         listModel.rest();
    }

    class ValEntryListModel extends AbstractListModel{
            public int getSize() {
                return selectedObjs.size();
            }
            public Object getElementAt(int index) {
                return selectedObjs.get(index);
            }

            public void add(ComboBoxValEntry obj){
                selectedObjs.add(obj);
                this.fireIntervalAdded(this, selectedObjs.size(), 0);
            }
            public void remove(int index){
                selectedObjs.remove(index);
                this.fireIntervalRemoved(this, index, index);
            }
            public void rest(){
                selectedObjs.clear();
                this.fireContentsChanged(this, 0, 0);
            }
     }

    public Object[] getSelectedObjs(){
        if(selectedObjs.isEmpty())
            return null;
        int i=selectedObjs.size();
        Object[] ret=new Object[i];
        for(i=0;i<selectedObjs.size();i++)
            ret[i]=((ComboBoxValEntry)selectedObjs.get(i)).getKey();
        return ret;
    }
}
